package com.dirk.happie.adapters;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.activities.ProfileActivity;
import com.dirk.happie.beans.AddressBean;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Keys;

public class AddressEditListAdapter extends ArrayAdapter<AddressBean> {
	Activity context;
	int layoutResourceId;
	private LayoutInflater inflater = null;
	private App mApp;

	public AddressEditListAdapter(Activity context, int layoutResourceId, ArrayList<AddressBean> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		inflater = context.getLayoutInflater();
		this.context = context;
		mApp = (App) context.getApplication();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ProductTypeHolder holder = null;

		if (row == null) {

			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ProductTypeHolder();
			holder.txtStreet = (TextView) row.findViewById(R.id.streetText);
			holder.txtApt = (TextView) row.findViewById(R.id.aptText);
			holder.txtZip = (TextView) row.findViewById(R.id.zipText);
			holder.txtCity = (TextView) row.findViewById(R.id.cityText);
			holder.imgDelete = (ImageView) row.findViewById(R.id.imgDelete);

			row.setTag(holder);
		} else {
			holder = (ProductTypeHolder) row.getTag();
		}

		holder.txtStreet.setText(getItem(position).getStreet());
		holder.txtApt.setText(getItem(position).getApt());
		holder.txtZip.setText(getItem(position).getZip());
		holder.txtCity.setText(getItem(position).getCity());

		holder.imgDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(context);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.dialog_sure);
				dialog.setCancelable(true);

				Button yesButton = (Button) dialog.findViewById(R.id.yes_button);
				yesButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						deleteAddress(position);
						notifyDataSetChanged();
						
						dialog.cancel();
					}
				});

				Button noButton = (Button) dialog.findViewById(R.id.no_button);
				noButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.cancel();
					}
				});

				dialog.show();
			}
		});

		return row;
	}

	static class ProductTypeHolder {
		TextView txtStreet;
		TextView txtApt;
		TextView txtZip;
		TextView txtCity;
		ImageView imgDelete;
	}

	private void deleteAddress(int position) {

		ArrayList<String> stringArray = new ArrayList<String>();
		try {
			JSONArray addressArray = new JSONArray(mApp.getHappie_prefs().getString(Keys.KEY_ADDRESSES, ""));
			for (int i = 0; i < addressArray.length(); i++) {
				stringArray.add(addressArray.getJSONObject(i).toString());
			}
			stringArray.remove(position);
			mApp.getHappie_prefs().edit().putString(Keys.KEY_ADDRESSES, stringArray.toString()).commit();
			remove(getItem(position));
			notifyDataSetChanged();
			((ProfileActivity) context).initListView();

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
}
